import { fetchAreaListApi, deleteCardApi, fetchRoleDropListApi, addAreaDropApi, updateAreaDropApi } from '@/api/car/areaApi';

const actions = {
  //用于发送请求获取区域列表的action
  async fetchAreaListAction(context, params) {
    const { data } = await fetchAreaListApi(params);
    context.commit('setAreaListMutation', data);
  },
  // 用于发送请求删除区域的action
  async deleteCardAction(context, id) {
    const { msg } = await deleteCardApi(id);
    return msg;
  },
  // 用于发送请求获取计费规则的action
  async fetchRoleDropListAction(context) {
    const { data } = await fetchRoleDropListApi();
    context.commit('setRoleDropListMutation', data);
  },
  // 用于发送请求添加区域的action
  async addAreaDropAction(context, data) {
    await addAreaDropApi(data);
    return '添加区域成功';
  },
  // 用于编辑区域的action
  async updateAreaDropAction(context, data) {
    await updateAreaDropApi(data)
    return '编辑区域成功';
  },
};

const mutations = {
  // 用于设置区域列表的mutation
  setAreaListMutation(state, { total, rows }) {
    state.total = total;
    state.areaList = rows;
  },
  // 用于设置计费规则的mutation
  setRoleDropListMutation(state, dropList) {
    state.dropList = dropList;
  },
};

const state = () => {
  return {
    areaList: [],
    total: 0,
    dropList: [],
  };
};

export default {
  actions,
  mutations,
  state,
  namespaced: true,
};
